<h3>Menu Access Area (Access level)</h3>
<div class="row-fluid">
	<div>
		<form action="<?php echo site_url('founder/menu/grant_access')?>" method="POST">
			<div class="row-fluid">
				<div class="span2">
					<h4>
						Menu Name
					</h4>
				</div>
				<div class="span10">
					<?php
					foreach($roles as $role)
					{
						?>
						<div class="span1" style="padding-left: 0px;" >
							<h5>
								<?php echo $role->name?>
							</h5>
						</div>
						<?php
					}?>
				</div>
			</div>		
			<div id="contentLeft">
				<ul class="ui-sortable" style="list-style:none;margin:0;">
					<!--Getting the heading names-->
					<?php foreach($menu_headings as $mh){?>
						<li id="recordsArray_<?= $mh->id?>" class="menu_border">
							<div class="row-fluid text-info menu_heading">
								<div class="span2">
									<?php echo $mh->name?>
								</div>
								<div class="span10" style="padding-left:12px;">
									<?php foreach($roles as $role){	?>
									<div class="span1">
									<?php
											$hcheck_id = $mh->id.$role->id;
											$hchecked  = null;
											foreach($heading_access as $hacc){
												if($hacc->heading_id.$hacc->group_id == $hcheck_id ){
													$hchecked = ' checked="checked"';
													break;
												}
											}
											?>
										<input type="checkbox"  value="<?php echo $hcheck_id?>" name="heading_menu_access[<?php echo $mh->id?>][<?php echo $role->id?>]" <?php echo $hchecked;?> /><label></label>
									</div>
									<?php }?>
								</div>								
							</div>
							<!--getting the menu under the heading-->
							<div id="main_menu">
							<ul class="h" type="none">
							
							<?php
							$main_menu = $this->menu_model->get_menu_by_heading_id($mh->id);
							//print_r($main_menu);
							foreach($main_menu as $mm)
							{?>
							<li id="recordsArray_<?= $mm->id?>">
							<div class="row-fluid">
								<div class="span2">
									<?php echo $mm->name?>
								</div>
								<div class="span10">
									<?php foreach($roles as $role){	?>
									<div class="span1">
									<?php
										$check_id = $mm->id.$role->id;
										$checked  = null;
										foreach($access as $acc){
											if($acc->menu_id.$acc->group_id == $check_id ){
												$checked = ' checked="checked"';
												break;
											}
										}?>
										<input type="checkbox" value="<?php echo $check_id?>" name="menu_access[<?php echo $mm->id?>][<?php echo $role->id?>]" <?php echo $checked;?> /><label></label>
									</div>
									<?php }?>
								</div>
							</div>							
							<!--child menu-->
							<?php
								$child_menus = $this->menu_model->get_child_menu($mm->id);
								foreach($child_menus as $cm){
								?>
								<div class="row-fluid">
									<div class="span2" style="padding-left: 10px">
									<?php echo $cm->name?>
									</div>
									<div class="span10">
									<?php foreach($roles as $role){	?>
									<div class="span1">
									<?php
										$check_id = $cm->id.$role->id;
										$checked  = null;
										foreach($access as $acc){
											if($acc->menu_id.$acc->group_id == $check_id ){
												$checked = ' checked="checked"';
												break;
											}
										}
									?>
										<input type="checkbox" value="<?php echo $check_id?>" name="menu_access[<?php echo $cm->id?>][<?php echo $role->id?>]" <?php echo $checked;?> /><label></label>
									</div>
									<?php }?>
								</div></div>
								<?php }?></li>
								<!--child menu-->
							<?php }?>
							<!--getting the menu under the heading ends here-->
							</ul></div>
						</li>
						<?php }?>
				</ul>
			</div>
			<br />			
			<input type="submit" class="btn btn-danger pull-right" value="save all access"/>
		</form>

	</div>
</div>
<hr />
<div class="row-fluid">
<h2>Steps in Creating Menu</h2>
	<div class="span4">
		<h4>Create Heading</h4>
		<div id="heading_error"  style="display:none; color: #b00000">
		</div>
		<hr/>
		<label>
		<input  type="text" id="heading_name" placeholder="Heading Name"/>
		</label>
		<label>
		<input  type="text" id="heading_icon" placeholder="Heading Icon"/></label>
		<label><input  type="button" class="btn btn-info" value="Create heading" id="create_heading" onclick="create_heading()"/></label>
	</div>
	<div class="span4">
		<form>
			<fieldset>
				<h4>Create Menu</h4>				
				<div id="error_div"  style="display:none; color: #b00000">
				</div>
				<hr/>
				<label><select class="chzn-select" id="heading">
					<option value="0">
						Select Heading
					</option>
					<?php
					foreach($menu_headings as $h)
					{
						?>
						<option value="<?php echo $h->id?>">
							<?php echo $h->name?>
						</option>
						<?php
					}?>
					$menu_headings
				</select></label>
				<label>
				<select class="chzn-select" id="parent">
					<option value="">
						Select Parent
					</option>
					<?php
					foreach($parent_menu as $parent)
					{
						?>
						<option value="<?php echo $parent->id?>">
							<?php echo $parent->name?>
						</option>
						<?php
					}?>
				</select></label>
				<label><input type="text" placeholder="Menu Name" id="name"></label>
				<label><input type="text" placeholder="controller/function" id="url"></label>
				<label><input type="text" placeholder="glyphicons  ex.icon-user" id="icon"></label>
				<label>
					<button type="button" class="btn btn-success" onclick="create_menu()">
						Create Menu
					</button>
				</label>
			</fieldset>
		</form>
	</div>

	<div class="span3">
		<h4>
			Edit
		</h4>
		<hr />
		<select class="chzn-select" onchange="getmenu_details(this.value)" id="id_menu">
			<option value="">
				Select Parent
			</option>
			<?php
			foreach($all_menu as  $parent)
			{
				?>
				<option value="<?php echo $parent->id?>">
					<?php echo $parent->name?>
				</option>
				<?php
			}?>
		</select>		
		<div id="edit_menu" style="display: none">
		</div>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function(e)
		{
			// drag n drop
			jQuery(function()
				{
					//alert("asdds");
					jQuery("#contentLeft .ui-sortable").sortable(
						{
							opacity: 0.6, cursor: 'move',  update: function()
							{
								var order = jQuery(this).sortable("serialize") + '&action=updateRecordsListings';
								//alert(order);
								jQuery.post("<?= site_url('founder/menu/sorting');?>", order, function(theResponse)
									{
										//alert(theResponse);
										location.reload();
									});
							}
						});
						
				});
					jQuery(function()
				{
				jQuery("#main_menu ul").sortable(
						{
							opacity: 0.6, cursor: 'move',  update: function()
							{
								var menuorder = jQuery(this).sortable("serialize") + '&action=updateRecordsListings';
								//alert(menuorder);
								jQuery.post("<?= site_url('founder/menu/main_menu_sort');?>", menuorder, function(theResponse)
									{
										//alert(theResponse);
										location.reload();
									});
							}
						});
						});
		});



	<!--

	function create_menu()
	{
		var name = $('#name').val();
		var urls = $('#url').val();
		var parent = $('#parent').val();
		var heading = $('#heading').val();
		var icon = $('#icon').val();
		var error='';
		//alert(name)
		//alert(urls)
		if(name != '' && urls != '' && heading != '' &&   icon != '' )
		{

			$.post('<?= site_url('founder/menu/create_menu');?>',
				{
					name : name , urls : urls, parents : parent, icon:icon, heading:heading
				}, function(data)
				{
					alert(data);
					$('#parent').val('');
					$('#name').val('');
					$('#url').val('');
					$('#heading').val('');
					location.reload();
				});

		}else
		{
			if(name == '' )
			{
				error=error + "Menu Name Field Should Not Be Empty!!<br>"
			}
			if(heading == '' )
			{
				error=error + "Heading Field Should Not Be Empty!!<br>"
			}
			if(urls == '' )
			{
				error = error + "Urls Field Should Not Be Empty!!<br>"
			}
			if(icon == '' )
			{
				error = error + "Icon Field Should Not Be Empty!!<br>"
			}

			$('#error_div').show();
			$('#error_div').html(error);
		}
		//alert(name);
	}
	/*Create heading*/
	function create_heading()
	{
		var name = $('#heading_name').val();
		var icon = $('#heading_icon').val();
		var heading_error = '';
		if( name != '' && icon !='')
		{
			$.post('<?= site_url('founder/menu/create_heading');?>',
				{
					name : name , icon:icon
				}, function(data)
				{
					alert(data);
					$('#heading_name').val('');
					$('#heading_icon').val('');
					location.reload();
				});

		}else
		{
			if(name == '')
			{
				heading_error = heading_error + 'Heading Name Should Not Be Empty!<br>';

			}
			if(icon == '')
			{
				heading_error = heading_error + 'Heading Icon Should Not Be Empty!<br>';

			}
			//	alert()
			$('#heading_error').show();
			$('#heading_error').html(heading_error);

		}
		//alert(name);
	}

	function access(role_id,menu_id)
	{

		//document.getElementById(role_id);
		if(this.checked)
		{
			alert(role_id);
		}
		else
		{
			alert(menu_id);
		}


		/*$.post('<?= site_url('');?>',{name : name , urls : urls  }, function(data) {
		$('#name').val('');
		$('#url').val('');
		location.reload();
		//alert(data);
		});*/
	}//-->

	function getmenu_details(id_menu)
	{
		$.post('<?= site_url('founder/menu/getmenu_details');?>',
			{
				id_menu : id_menu
			}, function(data)
			{

				//lert(data)
				$('#edit_menu').show()
				$('#edit_menu').html(data)

			});


	}

	function update_menu()
	{
		var name = $('#edit_name').val();
		var urls = $('#edit_url').val();
		var parent = $('#edit_parent').val();
		var heading = $('#edit_heading').val();
		var icon = $('#edit_icon').val();
		var edit_id = $('#edit_id').val();
		var error='';
		//alert(name)
		//alert(urls)
		if(name != '' && urls != '' && heading != '' &&   icon != '' )
		{

			$.post('<?= site_url('founder/menu/update_menu');?>',
				{
					name : name , urls : urls, parents : parent, icon:icon, heading:heading, edit_id : edit_id
				}, function(data)
				{
					alert(data);
					$('#edit_parent').val('');
					$('#edit_name').val('');
					$('#edit_url').val('');
					$('#edit_heading').val('');
					location.reload();
				});

		}else
		{
			if(name == '' )
			{
				error=error + "Menu Name Field Should Not Be Empty!!<br>"
			}
			if(heading == '' )
			{
				error=error + "Heading Field Should Not Be Empty!!<br>"
			}
			if(urls == '' )
			{
				error = error + "Urls Field Should Not Be Empty!!<br>"
			}
			if(icon == '' )
			{
				error = error + "Icon Field Should Not Be Empty!!<br>"
			}

			$('#edit_error_div').show();
			$('#edit_error_div').html(error);
		}
		//alert(name);
	}
</script>
